<?php
/**
* 无限分级递归函数
*/
class Category 
{
	
	/**
	 * 通過查詢所有分類信息,獲取分類列表, 拼接組合成一維數組
	 * @param  srting  $cate
	 * @param  string  $html
	 * @param  integer $parent_id
	 * @param  integer $level
	 * @return array
	 */
	static public function unlimitedForLevel($cate,$html='->',$parent_id=0,$level=0){

		$arr=array();
		foreach ($cate as  $v) {
			if($v['parent_id'] == $parent_id){
				$v['level'] = $level+1;
				$v['html'] = str_repeat($html, $level);
				$arr[] = $v;
				$arr = array_merge($arr,self::unlimitedForLevel(
					$cate,$html,$v['categories_id'],$level+1));
			}
		}
		return $arr;
	}

	
	/**
	 * 通過查詢所有分類信息,獲取分類列表, 合多维数组（无限分类）
	 * @param  [string  $cate
	 * @param  string  $name
	 * @param  integer $parent_id
	 * @return array
	 */
	static public function unlimitedForLayer($cate,$name='child',$parent_id=0){
		
		$arr = array();
		foreach($cate as $v){
			if($v['parent_id']==$parent_id){
				
				$v[$name]= self::unlimitedForLayer($cate,$name,$v['categories_id']);
				$arr[] = $v;
				
			}
		}
		return $arr;
	}
	/**
	 * 分類列表顯示  (exp: level1->level2->level3[->level4])
	 * @param  string  $cate
	 * @param  string  $name
	 * @param  integer $level
	 * @param  string  $html
	 * @return array
	 */
	static public function unlimitedForLayer2($cate,$name='',$level=0,$html='>'){

		$arr = array();
		$str = '';
		foreach($cate as $v){
			if($v['categories_status'] == 1){
				$status = '<span  class="hidden current"><img src="__PUBLIC__/images/icons/status_green.gif" />&nbsp;&nbsp;<img src="__PUBLIC__/images/icons/status_red_light.gif"  class="light"/></span>
               			   <span  class="hidden"><img src="__PUBLIC__/images/icons/status_green_light.gif" class="light" />&nbsp;&nbsp;<img src="__PUBLIC__/images/icons/status_red.gif" /></span>';
			}else{
				$status ='<span  class="hidden" ><img src="__PUBLIC__/images/icons/status_green.gif" />&nbsp;&nbsp;<img src="__PUBLIC__/images/icons/status_red_light.gif" class="light" /></span>
                   		  <span  class="hidden current"><img src="__PUBLIC__/images/icons/status_green_light.gif" class="light" />&nbsp;&nbsp;<img src="__PUBLIC__/images/icons/status_red.gif" /></span>';
			}
			if($v['parent_id'] == 0){
				$name = '';	
			}
			
			$str .= '<tr align="center" class="alt-row">'.
						'<td> <span><strong>'.$name.' '.$v['categories_description'][0]['categories_name'].'</strong></span></td>'.
						'<td align="center" class="status" id="'.$v['categories_id'].'">'.$status.'</td>'.
						'<td align="center" >'.$v['sort_order'].'</td>'.	
						'<td align="center" >   '.
                       		 '<a href="'.U('detail',array('id'=>$v['categories_id'])).'" title="Edit">Edit</a> &nbsp; &nbsp;         '.                     
                             '<a href="'.U('deleteCate',array('id'=>$v['categories_id'])).'"   onClick="return confirm(\'Delete this categories?\')"  title="Delete">Delete</a>'.
                         '</td>'.
						'</tr>';
			if(tep_not_null($v['child'])){
				$name .= $v['categories_description'][0]['categories_name'].' '.$html;
				$str .= self::unlimitedForLayer2($v['child'],$name,$level,$html='>');
			}
			
		}
		return $str;
	}
	/**
	 * 產品分類Tree
	 * @param  string  $cate_list
	 * @param  integer $parent
	 * @return string
	 */
	static public function get_products_to_category_tree($cate_list,$parent =0){
		$str ='';
		
		foreach($cate_list as $v){
			if($v['parent_id'] == $parent ){
				if($v['checked'] == 1){
					$checked = 'checked="checked"';	
				}else{
					$checked = '';
				}
				if($v['child'] > 0){
					$sub_ul = '<ul>';
					$sub_ul_last = '</ul>';
					$sub = 'class="folder"';
					$input = '';
				}else{
					$sub_ul = '';
					$sub_ul_last = '';
					$sub = '';
					$input = '<input name="categories_id[]" type="checkbox"  value="'.$v['categories_id'].'"  '.$checked.'/>';
				}
				$str .='<li><span '.$sub.'>'.$input.'&nbsp;'.$v['categories_description'][0]['categories_name'].'</span>';
				$str .= $sub_ul.''.self::get_products_to_category_tree($cate_list,$v['categories_id']).''.$sub_ul_last;
				$str .='</li>';
			}
		}
		return $str;
	}

	
	/**
	 * 传递一个子分类ID返回所有的夫级分类（导航）
	 * @param  string $cate
	 * @param  int $id
	 * @return array
	 */
	static public function getParents($cate,$id){
		$arr = array();
		foreach ($cate as $v) {
			if($v['categories_id'] == $id){
				$arr[] = $v;
				$arr = array_merge(self::getParents($cate,$v['parent_id']),$arr);
			}
		}
		return $arr;
	}

	/**
	 * 传递一个子分类ID返回所有的夫级分类（导航）
	 * @param  string $cate
	 * @param  int $parent_id
	 * @return array
	 */
	static public function getChildsId($cate,$parent_id){
		$arr = array();
		foreach ($cate as $v) {
			if($v['parent_id']==$parent_id){
				$arr[] = $v['categories_id'];
				$arr = array_merge($arr,self::getChildsId($cate,$v['categories_id']));
			}
		}
		return $arr;
	}
	/**
	 * 獲取分類列表Tree
	 * @param  string  $parent_id
	 * @param  string  $spacing
	 * @param  string  $exclude
	 * @param  string  $category_tree_array
	 * @param  boolean $include_itself
	 * @param  int  $default
	 * @return arrray
	 */
	static  public function tep_get_category_tree($parent_id = '0', $spacing = '', $exclude = '', $category_tree_array = '', $include_itself = false,$default) {
		if (!is_array($category_tree_array)) $category_tree_array = array();
		if ( (sizeof($category_tree_array) < 1) && ($exclude != '0') ) $category_tree_array[] = array('id' => '0', 'text' => 'Top');
	
		if ($include_itself) {
		  $category = M('categories_description')->where(array("categories_id " => (int)$parent_id ,"language_id" => (int)L('lan_id')))->find();
		  $category_tree_array[] = array('id' => $parent_id, 'text' => $category['categories_name']);
		}
		$categories = D('CategoryView')->getAll(array("c.parent_id " => (int)$parent_id ,"cd.language_id" => (int)L('lan_id')),'',"c.sort_order, cd.categories_name");
		
		foreach ($categories as $v) {
		  if ($exclude != $v['categories_id']){
		  
		    $category_tree_array[] = array('id' => $v['categories_id'], 'text' => $spacing . $v['categories_name']);
		  	$category_tree_array = self::tep_get_category_tree($v['categories_id'], $spacing . '&nbsp;&nbsp;&nbsp;',$exclude, $category_tree_array);
		  }
		}
		
	
		return $category_tree_array;
	  }

}

?>